<template>
  <div class></div>
</template>
<script>
import axios from "@/utils/requestdp";

export default {
  name: "APIqueryDbConnectorzz",
  components: {},
  data() {
    return {};
  },
  computed: {
    ishistory: function () {
      return this.$store.state.daping.ishistorytime;
    },
    timerangarr: function () {
      return this.$store.state.daping.timepick;
    },
  },
  watch: {
    "$store.state.daping.connectorList": {
      handler: function (n, o) {
        this.watchEvent();
      },
    },
  },
  mounted() {
  },
  methods: {
    watchEvent() {
      //先清除定时任务
      if (
        this.$store.state.daping.intervalArr["APIqueryDbConnectorzz"] != undefined
      ) {
        return;
      }
      let n_tmp = this.$store.state.daping.connectorList;
      this.loadData(n_tmp);
      let interval = setInterval(() => {
        let n_tmp = this.$store.state.daping.connectorList;
        this.loadData(n_tmp);
      }, this.$root.setting.data.interval);
      this.$store.state.daping.intervalArr["APIqueryDbConnectorzz"] = [
        { interval: interval },
      ];
    },
    loadData(addWaterName) {
      let _this = this;

      let queryJson = {
        queries: [
          {
            metric: "pid,m_009q",
            schema: "monitor",
          },
        ],
        orderBy: "pid",
        table: "connector", // "table": "supplystation",
      };

      if (this.ishistory == "历史") {
        queryJson = {
          queries: [
            {
              metric: "pid,m_009q",
              schema: "monitor",
            },
          ],
          orderBy: "pid",
          table: "connector",
          type: "history",
          startTime: this.timerangarr[0].getTime(),
          endTime: this.timerangarr[1].getTime(),
        };
      }

      axios.post("/open/queryDb", queryJson).then((res1) => {
        let data = res1.data;

        let addWaterData1 = [];
        let addWaterData2 = [];
        var yData = [];

        for (var i in data) {
          for (var j in addWaterName) {
            if (data[i]["pid"] == addWaterName[j]["pid"]) {
              if (addWaterName[j]["pid"] != undefined) {
                data[i]["name"] = addWaterName[j]["name"]
                  ? addWaterName[j]["name"]
                  : null;
              }
            }
          }
        }

        let ydataf = {};

        for (var i in data) {
          if (data[i]["name"]) {
            //pid==5 没有名字
            // yData.push(data[i]["name"]);

            var thisColor = data[i]["m_009q"] > 10000 ? "yellow" : "";
            if (!ydataf[data[i]["name"]]) {
              ydataf[data[i]["name"]] = {};
              ydataf[data[i]["name"]].value = [data[i]["m_009q"]];
              ydataf[data[i]["name"]].value1 = [0];
              ydataf[data[i]["name"]].color = thisColor;
            } else {
              ydataf[data[i]["name"]].value.push(data[i]["m_009q"]);
              ydataf[data[i]["name"]].value1.push(0);
            }

            //获取这个pid对应的限值 判断是否要加上color:"yellow"
            // var thisColor = data[i]["m_009q"] > 100 ? "yellow" : "";

            // addWaterData1.push({
            //   value: data[i]["m_009q"],
            //   color: thisColor
            // });

            // addWaterData2.push({
            //   value: 0,
            //   color: thisColor
            // });
          }
        }

        _.forIn(ydataf, (item) => {
          addWaterData1.push({
            value: (this.sum(item.value) / item.value.length).toFixed(0),
            color: item.color,
          });
          addWaterData2.push({
            value: (this.sum(item.value) / item.value.length).toFixed(0),
            color: item.color,
          });
        });

        yData = Object.keys(ydataf);
        this.$store.state.daping.queryDbConnectorzz = {
          addWaterData1: addWaterData1,
          addWaterData2: addWaterData2,
          yData: yData,
        };
      });
    },
    sum(arr) {
      return arr.reduce(function (prev, curr, idx, arr) {
        return prev + curr;
      });
    },
  },
};
</script>
<style lang='scss' scoped>
</style>